<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="12">
              <a-form-item label="表名">
                <a-input v-model="queryParam.key" placeholder="请输入搜索关键词" allow-clear/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="状态" prop="status">
                <a-select placeholder="请选择状态" style="width: 100%" allow-clear v-model="queryParam.status">
                  <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-list
        rowKey="tableName"
        :grid="{gutter: 24, lg: 3, md: 2, sm: 1, xs: 1}"
        :dataSource="list"
        class="card-list"
      >
        <a-list-item slot="renderItem" slot-scope="item">
          <template v-if="!item || item.tableName === undefined">
            <a-button class="new-btn" type="dashed" @click="$refs.createForm.handleAdd()">
              <a-icon type="plus"/>
              新增表
            </a-button>
          </template>
          <template v-else>
            <a-card :hoverable="true">
              <a-card-meta>
                <a slot="title">{{ item.tableLabel }}</a>
                <a-avatar class="card-avatar" slot="avatar" :src="tableIcon" size="large"/>
                <div class="meta-content" slot="description">
                  <p>表名： {{ item.tableName }}</p>
                  <p>备注： {{ item.remark }}</p>
                </div>
              </a-card-meta>
              <template class="ant-card-actions" slot="actions">
                <a-icon key="edit" type="edit" @click="$refs.createForm.handleUpdate(item)" />
                <a-icon key="setting" type="setting" @click="toGenEdit(item.tableName)" />
                <!-- <a-dropdown>
                  <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                    
                  </a>
                  <a-menu slot="overlay" @click="e => handleTableSetting(item, e)">
                    <a-menu-item key="column">
                      字段属性
                    </a-menu-item>
                    <a-menu-item key="page">
                      数据展示
                    </a-menu-item>
                    <a-menu-item key="form">
                      表单构建
                    </a-menu-item>
                  </a-menu>
                </a-dropdown> -->
                <a-icon key="delete" type="delete" @click="handleDelete(item.tableName)" />
              </template>
            </a-card>
          </template>
        </a-list-item>
      </a-list>

      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        @ok="handleOk"
      />

    </a-card>
  </page-header-wrapper>
</template>

<script>
import tableIcon from '@/assets/images/table-icon.png'
import CreateForm from './modules/CreateForm'
import { listTable, delTable } from '@/api/free/table'
export default {
  name: 'FreeTable',
  components: {
    CreateForm
  },
  data () {
    return {
      list: [],
      tableIcon: tableIcon,
      loading: false,
      total: 0,
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParam: {
        key: undefined,
        status: undefined
      }
    }
  },
  created () {
    this.getList()
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response
    })
  },
  methods: {
    /** 查询表集合 */
    getList () {
      this.loading = true
      listTable(this.addDateRange(this.queryParam, this.dateRange)).then(response => {
          this.list = [{}, ...response]
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.queryParam = {
        key: undefined,
        status: undefined
      }
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete (tableName) {
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '为保证数据安全，此操作仅删除代码生成的配置信息，数据库表请自行删除！',
        onOk: () => {
          return delTable(tableName).then(() => {
            this.$message.success(
              '删除成功',
              3
            )
            this.getList()
          })
        },
        onCancel () {}
      })
    },
    handleOk () {
      this.resetQuery()
    },
    toGenEdit (tableName) {
      this.$router.push({
        path: '/gen/edit',
        query: {
          tableName
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "~@/components/index.less";
.card-list {
  /deep/ .ant-card-body:hover {
    .ant-card-meta-title>a {
      color: @primary-color;
    }
  }
  /deep/ .ant-card-meta-title {
    margin-bottom: 12px;
    &>a {
      display: inline-block;
      max-width: 100%;
      color: rgba(0,0,0,.85);
    }
  }
  /deep/ .meta-content {
    position: relative;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    height: 40px;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    margin-bottom: 10px;
  }
}
.new-btn {
  background-color: #fff;
  border-radius: 2px;
  width: 100%;
  height: 175px;
}
.table-author {
  color: crimson;
}
</style>
